#!/bin/bash
set -o pipefail;

#TODO:activate when ready
# remove these comments and the exit statement; for now fail it
#exit 1

PGNAME=`basename $0`
PGDIRNAME=`dirname $0`
PGERR="$PGNAME[$$]: "
PGUSER=`id -un`


if [ "${MLPROD_HOME}" == "" ]
then
  echo "${PGERR} :  MLPROD_HOME env not set;  exiting !!!" >&2
  exit 10
fi

PGCFG_FILE="${MLPROD_HOME}/config/mlprog.rc"
if [ ! -f ${PGCFG_FILE} ]
then
  echo "${PGERR} : config file <${PGCFG_FILE}> not found; exiting !!!" >&2
  exit 10
fi

. "${PGCFG_FILE}"

CUST_CTX="$(mlget_cust_ctx 'cust')"
MOD_CTX="$(mlget_cust_ctx  'mod')"
BATCH_WFL_ID=1                  # workflow/product id
BATCH_MODE="oper"
if [ ! -z ${BATCH_RUN_MODE} ]
then
  BATCH_MODE="${BATCH_RUN_MODE}"
fi
LOG_CTX="${BATCH_MODE}-${CUST_CTX}"


PGCFG_FILE="${MLPROD_HOME}/cust/${CUST_CTX}/config/${MOD_CTX}.rc"
if [ ! -f ${PGCFG_FILE} ]
then
  echo "${PGERR} : config file <${PGCFG_FILE}> not found; exiting !!!" >&2
  exit 10
fi

. "${PGCFG_FILE}"
if [ ! -f ${PGCFG_FILE} ]
then
  echo "${PGERR} : config file sourcing <${PGCFG_FILE}> error; exiting !!!" >&2
  exit 10
fi


function cleanup
{
  rm -f ${EDER_DSTATUS_FILE};
  rm -f ${EDER_ESTATUS_FILE};

  if [ -f ${OUT_INSURANCE_FILE} ]
  then
    rm -f ${OUT_INSURANCE_FILE}
    if [ $? -ne 0 ]
    then
      mllogerr "[${LOG_CTX}] : ERR : Unable to cleanup files : ${OUT_INSURANCE_FILE}";
    fi
  fi

}

trap cleanup EXIT ERR TERM INT QUIT

function usage()
{
  mllogerr "Usage: $PGNAME -p [0|1|2] -b MMDDYY_HHMMSS_XXXXXX WHERE XXXXXX is cust_id";
}

if [ $# -ne 4 ]
then
  usage;
  exit 10;
fi


PART=-1
DBATE="";
while getopts "p:b:" opts
do
  case $opts in
    p ) PART=$OPTARG
        ;;
    b ) BDATE=$OPTARG
        ;;
    * ) usage; exit 10;
        ;;
  esac
done

if [ ${PART} -lt 0 -o ${PART} -gt 2  -o -z ${BDATE} ]
then
  usage
  exit 10
fi


# get the batch dates BATCH_RECV_DATE, BATCH_RUN_DATE, BATCH_EXEC_DATE
mlget_batch_dates "${BDATE}"
if [ $? -ne 0 ] || [ -z "${BATCH_RECV_DATE}" ]
then
  usage
  exit 10
fi

# LOG
LOG_CTX="${LOG_CTX}-${BATCH_RECV_DATE}"

# ACCOUNT FILE
IN_INSURANCE_FILE=${ARCH_CTX_BAK_DIR}/${BATCH_RECV_DATE}_batch_insurance0_t.gpg

# EQUIFAX FILE WITH SSN
IN_EFX_FILE=${ARCH_CTX_IN_DIR}/${BATCH_RECV_DATE}_efx.gpg

# SPOOL FILE
SPOOL=${MLFTP_DIR}/emdeon/spool/payorintel_ftp_request_${BATCH_RECV_DATE}


if [ ! -d ${LOG_CTX_DIR} ]
then
  mkdir -p ${LOG_CTX_DIR}
  if [ $? -ne 0 ]
  then
    mllogerr "[${LOG_CTX}] : ERR : Unable to create log folder : ${LOG_CTX_DIR}";
    exit 10;
  fi
fi


if [ ! -r ${IN_INSURANCE_FILE} ]
then
  mllogerr "[${LOG_CTX}] : ERR : batch insurance1 file not found : ${IN_INSURANCE_FILE}";
  exit 10;
fi


tstart=$((`date +%s`))

tstamp="00:00:00"
mllogerr "[${LOG_CTX}] : BEG : [${tstamp}] : BATCH START"


if [ $PART -eq 0 -o $PART -eq 1 ]
then
  ${PGDIRNAME}/run_part_1.sh ${BATCH_RECV_DATE}
  RET=$?
  if [ $RET -ne 0 ]
  then
    mllogerr "[${LOG_CTX}] : ERR : run batch part-1 failed"
    cleanup;
    exit $RET;
  fi
fi

# do emdeon only if part is = 0 (i.e whole process) 
# and we are runnng in production machine as user mlprod

if [ $PART -eq 0 -a "$PGUSER" == "mlprod" ] && [ $MMACHINE -eq 1 ]
then
  ML_tmp_dir="$MLPROD_HOME/tmp"
  exec 6>&1
  exec > $ML_tmp_dir/${BATCH_RECV_DATE}.tmp 
  ${PGDIRNAME}/get_emdeon_file_names.sh ${BATCH_RECV_DATE} "${SPOOL}" 
  GETFILE_RET=$?
  exec 1>&6 6>&-
  lFTPREQ=$(<$ML_tmp_dir/${BATCH_RECV_DATE}.tmp)
  rm -f $ML_tmp_dir/${BATCH_RECV_DATE}.tmp
  if [ $GETFILE_RET -eq 0 ]; then
	if [ -z $lFTPREQ ]; then
		mllogerr "[${LOG_CTX}] : INF : DB record not found";
	fi
	if [ ! -r ${SPOOL} ]; then
		mllogerr "[${LOG_CTX}] : ERR : $SPOOL  NOT CREATED";
		exit -1;
	fi
	chmod 0666 ${SPOOL}

	UPLOAD_FILE="${CUST_CTX}_${BATCH_RECV_DATE}_emdeon.txt.pgp"
    	EMDEON_UPLOAD_FILE=${MLARCH_DIR}/${CUST_CTX}/out/${UPLOAD_FILE}
    	NUMLINE=`cat ${EMDEON_UPLOAD_FILE} |wc -l`

    	if [ $NUMLINE -eq 0 ]
    	then
        	mllogerr "[${LOG_CTX}] : ERR : file is empty, no upload to emdeon"
         	exit -1;
    	else

	${MLBIN_DIR}/ftp_upload_emdeon_auto.sh "${CUST_CTX}" "${BATCH_RECV_DATE}"
	RET=$?
	if [ $RET -ne 0 ]; then
	  	mllogerr "[${LOG_CTX}] : ERR : upload emdeon part-1 failed "
		cleanup;
		exit -1;
	else
		mllogerr "[${LOG_CTX}] : SUCC : upload emdeon part-1 has sent request to emdeon "
	fi
       fi
  elif [ $GETFILE_RET -eq 1 ]; then
		mllogerr "[${LOG_CTX}] : INF : $SPOOL is already created";
  fi
fi


tstamp="$(mltstamp ${tstart})"
mllogerr "[${LOG_CTX}] : END : [${tstamp}] : BATCH DONE"

exit 0;
